package com.jier.service.impl;

import com.jier.common.Page;
import com.jier.common.Result;
import com.jier.common.util.StringUtil;
import com.jier.dao.SystemUserDao;
import com.jier.dto.SystemUserParamDto;
import com.jier.entities.SystemUser;
import com.jier.service.ISystemUserService;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * 系统用户业务实现
 *
 * @author jie_r
 * @date 2018/03/08
 */
@Service
public class SystemUserServiceImpl extends BaseServiceImpl<SystemUserDao, SystemUser> implements ISystemUserService {
    @Override
    public SystemUser login(String account, String password) throws Exception {
        Example example = getExample();
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("code", account);
        criteria.andEqualTo("password", password);
        criteria.andEqualTo("status", Boolean.TRUE);
        List<SystemUser> systemUsers = getByExample(example);
        if (StringUtil.isEmptyList(systemUsers)) {
            return null;
        }
        return systemUsers.get(0);
    }

    @Override
    public Result<SystemUser> getSystemUsers(SystemUserParamDto param, Page page) throws Exception {
        com.github.pagehelper.Page<SystemUser> listPage = getPageHelper(page).doSelectPage(() -> mapper.getSystemUsers(param));
        return getRecordPage(listPage, page);
    }

    @Override
    public SystemUser getSystemUserByCode(String code) throws Exception {
        Example example = getExample();
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("code", code);
        criteria.andEqualTo("status", Boolean.TRUE);
        return getUniqueByExample(example);
    }
}
